<?php
class MovieController extends Controller_Action_FrontAuth
{
	/**
	 * @var Db_Table_TMovie
	 */
	private $_dbMovie;
	
	private $_movieId;
	
	/**
	 * (non-PHPdoc)
	 * @see Zend_Controller_Action::preDispatch()
	 */
	public function preDispatch()
	{
		parent::preDispatch();
		$this->_dbMovie = new Db_Table_TMovie();
		$this->_movieId = $this->_getParam('movie_id');
		
		if(!ctype_digit("{$this->_movieId}")){
			return $this->_throw404Error();
		}
	}
	
	/**
	 * サムネイル出力
	 * 
	 * @return void
	 */
	public function thumbnailAction()
	{
		$db = $this->_dbMovie;
		$row = $db->getEnableAccessByMovieId($this->_loginUser, $this->_movieId);
		
		if(!$row || !strlen($row['thumbnail'])){
			return $this->_throw404Error();
		}
		
		$data = $row['thumbnail'];
		
		$response = $this->getResponse();
		$response->clearAllHeaders()->clearBody();
		$response->setHeader('Content-Type', 'image/jpeg');
		$response->setHeader('Content-Length', strlen($data));
				
		$response->setHeader('Expires', 0);
		$response->setRawHeader('Cache-Control:');
		$response->setRawHeader('Pragma:');
		
		$response->setBody($data);
		$response->sendResponse();
		exit;
	}
	
	/**
	 * FLV出力
	 * 
	 * @return void
	 */
	public function flvAction()
	{
		$db = $this->_dbMovie;
		$row = $db->getEnableAccessByMovieId($this->_loginUser, $this->_movieId);
		
		if(!$row || !strlen($row['filedata'])){
			return $this->_throw404Error();
		}
		
		$response = $this->getResponse();
		$response->clearAllHeaders()->clearBody();
		$response->setHeader('Content-Type', 'video/flv');
		$response->setHeader('Content-Length', $row['filesize']);
		
		$response->setBody($row['filedata']);
		$response->sendResponse();
		exit;
	}
	
	/**
	 * 表示アクション
	 *
	 * @return
	 */
	public function playAction()
	{
		$db = $this->_dbMovie;
		$row = $db->getEnableAccessByMovieId($this->_loginUser, $this->_movieId);
		
		if(!$row){
			return $this->_throw404Error();
		}
		
		$this->view->movie = $row;
	}
	
	public function deleteAction()
	{
		
	}
}